home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
6_10.lha
/
6_10
/
6_10in.c
< prev
next >
Wrap
Text File
|
1993-08-08
|
3KB
|
140 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
/ Input a LINT.
/ Convert a string of signed digits into a LINT.
/ Hex and octal prefixes are allowed.
include <ctype.h>
include <error.h>
include <lint.h>
/ convert hexadecimal 0-9, a-f, A-F
/ to its numeric value
nline int hexvalue(char c)
if (isdigit(c))
return c - '0';
else if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
else
return c - 'A' + 10;
/ return true if c is an octal digit
nline int isodigit(int c)
return isdigit(c) && c < '8';
/ perform: i = i * base - newdigit
tatic void addonedigit(LINT_type *i_s, int multiplier, int addend)
// i *= base
// almost identical to that used
// for operator*
LINT_Ltype k = 0;
for (int l = 3; ; )
{
/ cerr << "l=" << l << "\n"; /* DELETE */
/ cerr << "k=" << form("0x%8.8x", k) << "\n"; /* DELETE */
/ cerr << "s[l]=" << form("0x%4.4x", s[l]) << "\n"; /* DELETE */
LINT_Ltype t = i_s[l];
t *= multiplier;
t += k;
/ cerr << "t=" << form("0x%8.8x", t) << "\n"; /* DELETE */
i_s[l] = t; // % LINT_base
if (--l < 0)
break;
k = t / LINT_base;
}
// i -= addend
// almost identical to that used
// for operator-
k = addend;
for (l = 3; ; )
{
/ cerr << "l=" << l << "\n"; /* DELETE */
/ cerr << "k=" << form("0x%8.8x", k) << "\n"; /* DELETE */
/ cerr << "s[l]=" << form("0x%4.4x", s[l]) << "\n"; /* DELETE */
LINT_Ltype t = i_s[l] - k;
/ cerr << "t=" << form("0x%8.8x", t) << "\n"; /* DELETE */
i_s[l] = t;
if (--l < 0)
break;
k = (t / LINT_base) ? 1 : 0;
}
stream& operator>> (istream& in, LINT& i)
char c, sign = 0;
i = 0;
/ cerr << "starting input\n"; /* DELETE */
in >> WS; // skip white space
if (!in.get(c))
return in;
/ cerr << "got character '" << chr(c) << "'\n"; /* DELETE */
switch (c) // remember the sign
{
case '+': case '-':
sign = c;
/ cerr << "got sign\n"; /* DELETE */
if (!in.get(c))
return in;
/ cerr << "got character '" << chr(c) << "'\n"; /* DELETE */
break;
}
switch (c)
{
case '0': // hex or octal
/ cerr << "hex or octal\n"; /* DELETE */
if (!in.get(c))
break;
/ cerr << "got character '" << chr(c) << "'\n"; /* DELETE */
switch (c)
{
case 'x': // hex number
case 'X':
/ cerr << "hex\n"; /* DELETE */
for (in.get(c);
in && isxdigit(c);
in.get(c))
addonedigit(i.s, 16, hexvalue(c));
break;
default: // octal number
/ cerr << "octal\n"; /* DELETE */
for ( ; in && isodigit(c);
in.get(c))
addonedigit(i.s, 8, (c - '0'));
break;
}
break;
default: // decimal number
/ cerr << "decimal\n"; /* DELETE */
for ( ; in && isdigit(c); in.get(c))
addonedigit(i.s, 10, (c - '0'));
break;
}
if (in)
{ /* DELETE */
/ cerr << "putting back '" << chr(c) << "'\n"; /* DELETE */
in.putback(c);
} /* DELETE */
if (sign != '-')
{ /* DELETE */
/ cerr << "positive number\n"; /* DELETE */
i = -i;
} /* DELETE */
return in;